package com.woniuxy.creditsystem.systemmag.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.DownloadInfo;
import com.woniuxy.creditsystem.common.bean.ResultMsg;
import com.woniuxy.creditsystem.common.bean.TableDataBean;
import com.woniuxy.creditsystem.common.bean.UserInfo;
import com.woniuxy.creditsystem.common.util.TraceUtil;
import com.woniuxy.creditsystem.systemmag.service.IDownloadService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.xml.transform.Source;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 下载中心的增、查
 * @author hejie
 */
@RequestMapping("/download")
@RestController
public class DownloadController {

    @Resource
    private IDownloadService downloadServiceImpl;

    //从application.properties中获取poi的excel文件存放路径
    @Value("${poiFilePath}")
    private String poiFilePath;

    /**
     * 把前台传回来的table中的DownloadInfo list集合 导出到一个Excel文件
     * @author hejie
     * @param tableDataBean= tableDataJson(前端表格数据)+ simpleFileNameUserSet（用户定义的表格名字）
     * @return ResultMsg
     */
   @PostMapping(value="/exportList",produces = {"application/json;charset=utf-8"})
    public ResultMsg tableDataJson(@RequestBody TableDataBean<DownloadInfo> tableDataBean ) throws IOException {
       List<DownloadInfo> tableDataJson=tableDataBean.getTableDataJson();
       String simpleFileNameUserSet=tableDataBean.getSimpleFileNameUserSet();

        downloadServiceImpl.write2excel(tableDataJson,poiFilePath,simpleFileNameUserSet);

       System.out.println("\n\nsimpleFileNameUserSet="+simpleFileNameUserSet+TraceUtil.positionTrace());
        System.out.println("\n"+TraceUtil.positionTrace());
       System.out.println("\ntableDataJson="+tableDataJson);
        for (DownloadInfo downloadInfo:tableDataJson) {
            System.out.println(downloadInfo);
        }
        ResultMsg resultMsg = new ResultMsg(200,"in tableDataJson()",null);
        return resultMsg;
    }

    /**
     * 新增下载
     * @author hejie
     * @param dataSourceId 数据源的id
     * @param reportName 用户定义的表格名字
     * @return
     */
    @RequestMapping(value="/newDownload",produces = {"application/json;charset=utf-8"})
    public ResultMsg newDownload(Long dataSourceId,String reportName) throws IOException {

        System.out.println("\ndataSourceId=***"+dataSourceId+"\treportName="+reportName+TraceUtil.positionTrace());
        ResultMsg<String> reMsg= downloadServiceImpl.newDownload(dataSourceId,reportName);
        String fileNameStr=reMsg.getData();
        ResultMsg resultMsg = new ResultMsg(200,"新增下载成功",fileNameStr);
        return resultMsg;
    }

    /**
     * 分页查询数据
     * @author hejie
     * @param downloadInfo 查询数据
     * @return
     */
    @RequestMapping(value="/page",produces = {"application/json;charset=utf-8"})
    public PageInfo<DownloadInfo> findAllByParams(int pageNum, DownloadInfo downloadInfo,String userInfoUserName){
        System.out.println("\ninfindAllByParams..."+downloadInfo);

        //把前台传过来的的操作用户名字，设置到systemLogInfo的userInfo中
        if (downloadInfo!=null && userInfoUserName!=null){
            UserInfo userInfo= new UserInfo();
            userInfo.setUserName(userInfoUserName);
            downloadInfo.setUserInfo(userInfo);
        }
        //每页显示多少条数据
        int pageSize=10;
        String createTimeStr=null;
        if (downloadInfo.getCreateTime()!=null){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
            createTimeStr=sdf.format(downloadInfo.getCreateTime());
            System.out.println("\ncreateTimeStr="+createTimeStr);
        }
        PageInfo<DownloadInfo> pageInfo = downloadServiceImpl.findAllByParams(pageNum,pageSize,downloadInfo,createTimeStr);
        System.out.println("\npageInfo.getList()="+pageInfo.getList()+TraceUtil.positionTrace());
        return pageInfo;
    }
}
